function [vote_per_reg_cell,confidence_cell]=vote_per_reg(reg_features_cell,models_struct)

% This function returns for each region what is the label it belongs to,according to the models struct.

% Function Inputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% reg_features_cell - cell that contains all the regions' features, the output of calc_features_reg.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
% models_struct -  struct of models that are the output from a classifier.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Function Outputs:
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%  vote_per_reg_cell - cell array with labels for each region in each hypothesis.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
%  confidence_cell - cell array with confidence number for each region label' decision. 
% if the classifier won't give confidences, then confidence=0.
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

% Global Inputs:
% model_p.class_func
% label_p.hypo_amount
% label_p.norm_prob

global model_p
global label_p
hypo_amount=label_p.hypo_amount;
num_of_hypo_groups=size(label_p.reg_per_hypo,2);
class_func=model_p.class_func;
norm_prob=label_p.norm_prob;

vote_per_reg_cell = cell(num_of_hypo_groups,1);
confidence_cell = cell(num_of_hypo_groups,1);

for i=1:num_of_hypo_groups
    vote_per_reg_cell{i} = cell(hypo_amount,1);
    confidence_cell{i} = cell(hypo_amount,1);
    % running for each group of hypothesis, for each hypothesis
    for k=1:hypo_amount
        temp_reg_features_cell = reg_features_cell{i}{k};
        vote_per_reg_cell{i}{k} = zeros(1,temp_reg_features_cell.reg_count);
        confidence_cell{i}{k} = zeros(1,temp_reg_features_cell.reg_count);
        % setting all features together
        temp_X = [temp_reg_features_cell.reg_loc_mat;temp_reg_features_cell.reg_text_mat;temp_reg_features_cell.reg_color_mat;
                              temp_reg_features_cell.reg_geom_mat;temp_reg_features_cell.reg_shape_mat];

        if strcmp(class_func,'KNN')
            vote_per_reg_cell{i}{k}=knn_classify(temp_X,models_struct);
        end % end of KNN

        if strcmp(class_func,'FLD')
            % running Fisher linear classifier with one against all classifier
            [ypred_1,conf_1] = fld_classify(temp_X,models_struct.model_1); % 1 if 1, 2 if others
            [ypred_2,conf_2] = fld_classify(temp_X,models_struct.model_2); % 1 if 2, 2 if others
            [ypred_3,conf_3] = fld_classify(temp_X,models_struct.model_3); % 1 if 3, 2 if others
            [ypred_4,conf_4] = fld_classify(temp_X,models_struct.model_4); % 1 if 4, 2 if others
            ypred=[ypred_1;ypred_2;ypred_3;ypred_4];

            %normalizing the probabilities
            if strcmp(norm_prob,'true')
                prob=[fld2prob(conf_1);fld2prob(conf_2);fld2prob(conf_3);fld2prob(conf_4)];
            else
                prob=[conf_1;conf_2;conf_3;conf_4];
            end
            [max_value,label_indices]=max(prob.*(ypred==1),[],1); % getting best score only from labels 'one'
            vote_per_reg_cell{i}{k}=label_indices; %setting label by the best score.
            confidence_cell{i}{k}=max_value;
            vote_per_reg_cell{i}{k}(max_value==0)=0; % maybe all labels are 'all'
            confidence_cell{i}{k}(max_value==0)=0; 
        end % end of FLD

        if (strcmp(class_func,'RealAdaBoost') || strcmp(class_func,'ModestAdaBoost'))
            % running AdaBoost classifier with desicions tree as weak learner
            weak_learner=models_struct.weak_learner;
            if (isstruct(weak_learner))
                [ypred_1] = adaboost_struct_classify(models_struct.model_1.Learners,models_struct.model_1.Weights,temp_X); % 1 if 1, -1 if others
                [ypred_2] = adaboost_struct_classify(models_struct.model_2.Learners,models_struct.model_2.Weights,temp_X); % 1 if 2, -1 if others
                [ypred_3] = adaboost_struct_classify(models_struct.model_3.Learners,models_struct.model_3.Weights,temp_X); % 1 if 3, -1 if others
                [ypred_4] = adaboost_struct_classify(models_struct.model_4.Learners,models_struct.model_4.Weights,temp_X); % 1 if 4, -1 if others
            else
                [ypred_1] = adaboost_classify(models_struct.model_1.Learners,models_struct.model_1.Weights,temp_X); % 1 if 1, -1 if others
                [ypred_2] = adaboost_classify(models_struct.model_2.Learners,models_struct.model_2.Weights,temp_X); % 1 if 2, -1 if others
                [ypred_3] = adaboost_classify(models_struct.model_3.Learners,models_struct.model_3.Weights,temp_X); % 1 if 3, -1 if others
                [ypred_4] = adaboost_classify(models_struct.model_4.Learners,models_struct.model_4.Weights,temp_X); % 1 if 4, -1 if others
            end
            ypred=[ypred_1;ypred_2;ypred_3;ypred_4];

            %Voting by max value
            [max_value,label_indices]=max(ypred,[],1); % getting best score only from labels 'one'
            vote_per_reg_cell{i}{k}=label_indices; %setting label by the best score.
            confidence_cell{i}{k}=max_value;
            vote_per_reg_cell{i}{k}(max_value<0)=0; % maybe all labels are 'all'
            confidence_cell{i}{k}(max_value==0)=0; 
        end % end of AdaBoost

    end
end
